import java.util.ArrayList;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Phase-based method configuration dialog
 * @author Administrator
 *
 */
public class PBConfigDlg extends Dialog{

	public PBConfigDlg(Shell parent) {
		this(parent,SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		// TODO Auto-generated constructor stub
	}
	public PBConfigDlg(Shell parent, int style) {
		super(parent, style);
		// TODO Auto-generated constructor stub
	}
	public void open(){
		Display display = getParent().getDisplay();
		Shell shell = new Shell(getParent(),getStyle());
		shell.setText("Phase-Based Strategy");
		createContents(shell);
		shell.pack();
		int x = getParent().getBounds().x + (getParent().getBounds().width - shell.getBounds().width)/2;
		int y = getParent().getBounds().y + (getParent().getBounds().height - shell.getBounds().height)/2;
		shell.setLocation(x,y);
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
	}
	private void createContents(final Shell shell){
		shell.setLayout(new GridLayout(3,false));
		final int typeNum = AllBasicData.roles.length;
		final Button[] checkBtns = new Button[typeNum];
		final Label[] roleLbls = new Label[typeNum];
		final Combo[] siteCbs = new Combo[typeNum];
		for(int i=0;i<typeNum;i++){
			checkBtns[i] = new Button(shell,SWT.CHECK);
			roleLbls[i] = new Label(shell,SWT.NONE);
			roleLbls[i].setText(AllBasicData.roles[i]);
			siteCbs[i] = new Combo(shell,SWT.DROP_DOWN|SWT.READ_ONLY);
		}
		//fill the countries strings to combo box
		Set<Integer> countries = MainGUI.getApp().getSiteCountries();
		//map the country id to list index
		final ArrayList<Integer> counList = new ArrayList<Integer>();
		if(countries!=null){
			String[] counstr = new String[countries.size()];
			int i = 0;
			for(Integer id:countries){
				counstr[i++]=AllBasicData.countries[id].replace('_', ' ');
				counList.add(id);
			}
			for(i=0;i<typeNum;i++)
				siteCbs[i].setItems(counstr);
		}
		shell.addDisposeListener(new DisposeListener(){
			public void widgetDisposed(DisposeEvent arg0) {
				// TODO Auto-generated method stub
				int[] cfg = new int[typeNum];
				boolean correctSelection = false;
				for(int i=0;i<typeNum;i++){
					if(checkBtns[i].getSelection()&&siteCbs[i].getSelectionIndex()>=0){
						cfg[i] = counList.get(siteCbs[i].getSelectionIndex());
						correctSelection = true;
					}else{
						cfg[i] = -1;
					}
				}
				if(correctSelection==true)//if the selection is a valid configuration
					AllBasicData.pbMap = cfg;
				else
					AllBasicData.pbMap = null;
			}});
		//initialization with proper basic configuration data
		if(AllBasicData.pbMap!=null){
			for(int i=0;i<typeNum;i++){
				if(AllBasicData.pbMap[i]>=0){
					checkBtns[i].setSelection(true);
					siteCbs[i].select(AllBasicData.pbMap[i]);
				}else{
					checkBtns[i].setSelection(false);
					siteCbs[i].select(-1);
				}
			}
		}
	}
	public static void main(String[] args) throws Exception{
		Display display = new Display();
		Shell shell = new Shell(display);
		new PBConfigDlg(shell,SWT.DIALOG_TRIM).open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
